package com.xukai.springboot.controller.statistics;

import com.xukai.springboot.datalayer.statis.DataSourceMapper;
import com.xukai.springboot.entity.request.statis.DelDataSourceRequest;
import com.xukai.springboot.entity.request.statis.UpdateDataSourceStateRequest;
import com.xukai.springboot.entity.response.PageResult;
import com.xukai.springboot.entity.response.ResponseBase;
import com.xukai.springboot.entity.statistics.t_statis_datasource;
import com.xukai.springboot.quartzTask.QuartzTaskManager;
import com.xukai.springboot.quartzTask.UpdateStatisTask;
import com.xukai.springboot.service.statistics.DataSourceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(description = "数据源管理", basePath = "api/datasource")
@RestController
@RequestMapping("api/datasource")
public class DataSourceController {

    /**
     * 日志
     */
    private final Logger logger = LoggerFactory.getLogger(DataSourceController.class);

    @Autowired
    DataSourceService dataSourceService;

    @Autowired
    QuartzTaskManager quartzTaskManager;


    /**
     * 获取数据源列表
     */
    @ApiOperation(value = "获取数据源列表", notes = "获取数据源列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "state", value = "数据源状态", required = true, dataType = "int", example = "0", defaultValue = "0", paramType = "query"),
            @ApiImplicitParam(name = "startTime", value = "开始时间", required = true, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "endTime", value = "结束时间", required = true, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "dataSourceName", value = "数据源名称", required = false, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "检索数据总数，如果要检索全部数据传值-1", required = false, example = "0", defaultValue = "0", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "startCount", value = "数据开始行", required = false, dataType = "int", example = "0", defaultValue = "0", paramType = "query")
    })
    @GetMapping("GetDataSourceList")
    public ResponseBase<PageResult<t_statis_datasource>> GetDataSourceList(@RequestParam int state, @RequestParam String startTime, @RequestParam String endTime
            , @RequestParam String dataSourceName, @RequestParam int pageSize, @RequestParam int startCount) {
        ResponseBase<PageResult<t_statis_datasource>> result = new ResponseBase<>();
        try {
            PageResult<t_statis_datasource> pageResult = new PageResult<>();
            List<t_statis_datasource> data = dataSourceService.GetDataSourceList(state, startTime, endTime, dataSourceName, pageSize, startCount);
            pageResult.setResultData(data);
            int count = dataSourceService.GetDataSourceCount(state, startTime, endTime, dataSourceName);
            pageResult.setTotal(count);
            result.setData(pageResult);
        } catch (Exception e) {
            logger.error("获取数据源列表", e);
            result.setStatue(0);
        }
        return result;

    }

    /**
     * 新建数据源
     */
    @ApiOperation(value = "新建数据源", notes = "新建数据源")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "param", value = "数据源数据", required = true, dataType = "t_statis_datasource", paramType = "body")
    })
    @PostMapping("AddDataSource")
    public ResponseBase<t_statis_datasource> AddDataSource(@RequestBody t_statis_datasource param) {
        ResponseBase<t_statis_datasource> result = new ResponseBase<>();
        try {
            t_statis_datasource data = dataSourceService.AddDataSource(param);
            result.setData(data);
            //添加新的任务
            quartzTaskManager.AddTask(String.valueOf(data.getId()), String.valueOf(data.getId()), UpdateStatisTask.class, data.getDataRefreshTime());
        } catch (Exception e) {
            logger.error("新建数据源", e);
            result.setStatue(0);
        }

        return result;
    }

    /**
     * 修改数据源
     */
    @ApiOperation(value = "修改数据源", notes = "修改数据源")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "param", value = "数据源数据", required = true, dataType = "t_statis_datasource", paramType = "body")
    })
    @PostMapping("UpdateDataSource")
    public ResponseBase UpdateDataSource(@RequestBody t_statis_datasource param) {
        ResponseBase result = new ResponseBase();
        try {
            dataSourceService.UpdateDataSource(param);
            //改任务时间
            quartzTaskManager.ModifeTask(String.valueOf(param.getId()), String.valueOf(param.getId()), param.getDataRefreshTime());
        } catch (Exception e) {
            logger.error("修改数据源", e);
            result.setStatue(0);
        }
        return result;
    }

    /**
     * 删除数据源
     */
    @ApiOperation(value = "删除数据源", notes = "删除数据源")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "param", value = "删除数据源参数", required = true, dataType = "DelDataSourceRequest", paramType = "body")
    })
    @PostMapping("DelDataSource")
    public ResponseBase DelDataSource(@RequestBody DelDataSourceRequest param) {
        ResponseBase result = new ResponseBase();
        try {
            dataSourceService.DelDataSource(param.getId());
            //删除定时任务
            quartzTaskManager.RemoveTask(String.valueOf(param.getId()), String.valueOf(param.getId()));
        } catch (Exception e) {
            logger.error("删除数据源", e);
            result.setStatue(0);
        }
        return result;
    }

    /**
     * 获取数据源详细信息
     */
    @ApiOperation(value = "删除数据源", notes = "删除数据源")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "数据源ID", required = true, example = "0", defaultValue = "0", dataType = "int", paramType = "query")
    })
    @GetMapping("GetDataSource")
    public ResponseBase<t_statis_datasource> GetDataSource(int id) {
        ResponseBase<t_statis_datasource> result = new ResponseBase<>();
        try {

        } catch (Exception e) {
            logger.error("获取数据源详细信息", e);
            result.setStatue(0);
        }
        return result;
    }

    /**
     * 修改数据源状态
     */
    @ApiOperation(value = "修改数据源状态", notes = "修改数据源状态")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "param", value = "修改数据状态参数", required = true, dataType = "UpdateDataSourceStateRequest", paramType = "body")
    })
    @PostMapping("UpdateState")
    public ResponseBase UpdateState(@RequestBody UpdateDataSourceStateRequest param) {
        ResponseBase result = new ResponseBase();
        try {
            dataSourceService.UpdateState(param.getId(), param.getState());
            if(param.getState()==1)
            {
                //启动定时任务
                quartzTaskManager.ResumeTask(String.valueOf(param.getId()), String.valueOf(param.getId()));
            }else{
                //暂停定时任务
                quartzTaskManager.PauseTask(String.valueOf(param.getId()), String.valueOf(param.getId()));
            }
        } catch (Exception e) {
            logger.error("修改数据源状态", e);
            result.setStatue(0);
        }
        return result;

    }

    /**
     * 验证数据源名称是否存在
     *
     * @param id
     * @param dataSourceName
     * @return
     */
    @ApiOperation(value = "验证数据源名称是否存在", notes = "验证数据源名称是否存在")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "数据源ID", required = true, example = "0", defaultValue = "0", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "dataSourceName", value = "数据源名称", required = true, dataType = "string", paramType = "query")
    })
    @GetMapping("IsExist")
    public ResponseBase<Boolean> IsExist(@RequestParam int id, @RequestParam String dataSourceName) {
        ResponseBase<Boolean> result = new ResponseBase<>();
        try {
            result.setData(dataSourceService.IsExist(id, dataSourceName));
        } catch (Exception e) {
            logger.error("修改数据源状态", e);
            result.setStatue(0);
        }
        return result;
    }


    /**
     * 刷新数据
     */
    @ApiOperation(value = "刷新数据", notes = "刷新数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "param", value = "数据源数据", required = true, dataType = "t_statis_datasource", paramType = "body")
    })
    @PostMapping("RefreshData")
    public ResponseBase RefreshData(@RequestBody t_statis_datasource param) {
        ResponseBase result = new ResponseBase();
        try {
            dataSourceService.RefreshData(param.getDataSourceName(), param.getResultType(), param.getId(), param.getSql());
        } catch (Exception e) {
            logger.error("修改数据源状态", e);
            result.setStatue(0);
        }
        return result;
    }


}
